function AllReadyList() {
    console.log("11");
    // Ext.getCmp("viewPort").items.items[0].insert(24, {
    //     itemId: "demb",
    //     text: "保存调整时间",
    //     width: this.itemWidth,
    //     iconCls: `iconfont iconsave`,
    // }); //指定位置插入按钮
    setTimeout(function () {
        const mList = Ext.getCmp("KcBillheadList");
        const mList1 = Ext.getCmp("KcBillbodyList");

        const toolbar = Ext.getCmp("ext-comp-1036");

        toolbar.insert(24, {
            itemId: "demb-t",
            text: "验收确认时间",
            width: this.itemWidth,
            iconCls: "iconfont iconsave", //按钮形状
        });

        toolbar.insert(24, {
            itemId: "demb",
            text: "安调确认时间",
            width: this.itemWidth,
            iconCls: "iconfont iconsave", //按钮形状
        });
        toolbar.get("demb").on("click", function () {
            try {
                createWindow(OkBtn1)?.show();
            } catch (error) {
                Ext.Msg.alert("提示", "修改安调时间出现错误请联系服务器客服");
            }
        });
        toolbar.get("demb-t").on("click", function () {
            // const PhId = selected1[0].data.PhId

            try {
                createWindow(OkBtn2)?.show();
            } catch (error) {
                Ext.Msg.alert("提示", "修改验收时间出现错误请联系服务器客服");
            }
        });
    }, 200);
}

//确认
function OkBtn1(datePicker, window) {
    const selectedDate = datePicker.getValue();
    const formattedDate = Ext.Date.format(selectedDate, "Y-m-d");
    const dgrid = Ext.getCmp("KcBillbodyList");
    if (!dgrid) {
        Ext.Msg.alert("提示", "请先显示明细！");
        window.close();
        return;
    }
    const selectedRows = dgrid.getSelectionModel().getSelection();
    let cheakRrr = [];
    // dgrid.getStore().each(item=>{
    //     cheakRrr.push(item.get("PhId"))
    // })
    selectedRows.forEach((row) => {
        cheakRrr.push(row.get("PhId")); // 收集选中行的 PhId
    });
    if (cheakRrr.length == 0) {
        Ext.Msg.alert("提示", "请先显示明细！");
        window.close();
        return;
    }
    console.log(`全部行PHID`, cheakRrr);
    try {
        execServer("updateTzsj", { phid: `(${cheakRrr.join(",")})`, date: formattedDate }, function (res) {
            if (res.status == "success") {
                Ext.Msg.alert("提示", "状态更新成功，请刷新页面查看");
            } else {
                Ext.Msg.alert("提示", "状态更失败，请刷新页面查看");
            }
        });
    } catch (er) {
        Ext.Msg.alert("提示", "请先显示明细！");
    }
    // toolbar.get("save").enable()
    // 关闭弹窗
    window.close();
}
function OkBtn2(datePicker, window) {
    const selectedDate = datePicker.getValue();
    const formattedDate = Ext.Date.format(selectedDate, "Y-m-d");
    const dgrid = Ext.getCmp("KcBillbodyList");
    if (!dgrid) {
        Ext.Msg.alert("提示", "请先显示明细！");
        window.close();
        return;
    }
    const selectedRows = dgrid.getSelectionModel().getSelection();
    const cheakRrr = [];
    // dgrid.getStore().each(item=>{
    //     cheakRrr.push(item.get("PhId"))
    // })
    selectedRows.forEach((row) => {
        cheakRrr.push(row.get("PhId")); // 收集选中行的 PhId
    });
    if (cheakRrr.length == 0) {
        Ext.Msg.alert("提示", "请先显示明细！");
        window.close();
        return;
    }
    try {
        execServer("updateYsDate", { phid: `(${cheakRrr.join(",")})`, date: formattedDate }, function (res) {
            if (res.status == "success") {
                Ext.Msg.alert("提示", "状态更新成功，请刷新页面查看");
            } else {
                Ext.Msg.alert("提示", "状态更失败，请刷新页面查看");
            }
        });
    } catch (err) {
        Ext.Msg.alert("提示", "请先显示明细！");
    }
    // toolbar.get("save").enable()
    // 关闭弹窗
    window.close();
}
function createWindow(fun) {
    // 创建提示标签
    const label = Ext.create("Ext.form.Label", {
        text: "请选择要重置的时间",
        style: "margin-bottom: 10px;",
    });

    // 创建日期选择器
    const datePicker = Ext.create("Ext.picker.Date", {
        format: "Y-m-d", // 设置日期格式
        width: 220,
    });

    // 创建确认按钮
    const okButton = Ext.create("Ext.button.Button", {
        text: "确定",
        handler: () => {
            fun(datePicker, window);
        },
    });

    // 创建取消按钮
    const cancelButton = Ext.create("Ext.button.Button", {
        text: "取消",
        handler: function () {
            // 关闭弹窗
            window.close();
        },
    });

    // 创建弹窗
    const window = Ext.create("Ext.window.Window", {
        title: "请选择日期",
        width: 300,
        height: 315,
        layout: "fit", // 使用 vbox 布局，以便垂直排列组件
        items: [datePicker],
        buttons: [okButton, cancelButton],
        modal: true, // 设置为模态窗口
    });
    return window;
}
function AllReady() {
    var mstform = Ext.getCmp("KC_INKCGRK_Head"); //获取容器
    var dgrid = Ext.getCmp("KC_INKCGRK_Body");
    var Toolbar = Ext.getCmp("toolbar");
    mstform.on("dataready", function () {
        console.log("Data ready event triggered");
        if (otype == "view") {
            console.log("view");
            Toolbar.get("save").disabled = false;
            dgrid.setReadOnlyCol("user_atdt", false);
        }
    });
    console.log("11111111111111111111111111111");

    var dstore = dgrid.store;

    var PhidTrProj = mstform.queryById("PhidTrProj"); //项目工程
    var user_pc_no = mstform.queryById("user_pc_no"); //项目编号
    var user_division = mstform.queryById("user_division"); //获取字段
    var user_product_line = mstform.queryById("user_product_line");

    var user_yhd_name = mstform.queryById("user_yhd_name");
    var user_yhd_no = mstform.queryById("user_yhd_no");
    var user_yhd_dt = mstform.queryById("user_yhd_dt");
    var user_yhd_amt = mstform.queryById("user_yhd_amt"); //要货单金额--不含税
    var PhidContractno = mstform.queryById("PhidContractno"); //合同编码
    var ContractName = mstform.queryById("ContractName");
    var user_cnt_amt = mstform.queryById("user_cnt_amt");
    var PhidCompno = mstform.queryById("PhidCompno");
    var user_regional_center = mstform.queryById("user_regional_center");
    var user_sl_organize = mstform.queryById("user_sl_organize"); //收料组织
    var PhidDeptno = mstform.queryById("PhidDeptno"); //收料部门
    var user_ck_no = mstform.queryById("user_ck_no");
    var PhidChkpsn = mstform.queryById("PhidChkpsn");
    var user_bill_type = mstform.queryById("user_bill_type");
    var PhidOcode = mstform.queryById("PhidOcode");
    var user_xsht_name = mstform.queryById("user_xsht_name");
    var user_xsht_no = mstform.queryById("user_xsht_no");
    var PhidWarehouse = mstform.queryById("PhidWarehouse"); //新增时根据方案自动生成不同的汇总层级

    // var phid_schemeid = mstform.getItem("phid_schemeid"); //方案ID

    // if (otype == "add") {
    // 	console.log(phid_schemeid.getValue(), "phid_schemeid.getValue()");

    // 	// if (phid_schemeid.getValue() == "747220909017002") {
    // 	// 	u_cj.setValue("2");
    // 	// 	BatchBindCombox([u_cj]);
    // 	// } else if (phid_schemeid.getValue() == "747220909023003") {
    // 	// 	u_cj.setValue("1");
    // 	// 	BatchBindCombox([u_cj]);
    // 	// }
    // }
    //   if (otype == "add") {
    //     setTimeout(() => {
    //       PhidOcode.setValue("");
    //       BatchBindCombox([PhidOcode]);
    //     }, 200);
    //   }
    // 获取项目信息
    otype != 'view' && getPtInfo()
    function getPtInfo() {
        if(!PhidTrProj.getValue()) return 
        execServer("getPtInfoByPhid", { phid: PhidTrProj.getValue() }, function (res) {
            const obj = JSON.parse(res.data)[0].extendObjects;
            console.log(obj, "obj");
            user_division.setValue(obj.division_phid);
            PhidOcode.setValue(obj.org_phid);
            user_product_line.setValue(obj.midwife_phid);
            user_regional_center.setValue(obj.regional_center_phid);
            user_pc_no.setValue(obj.pc_no);
            BatchBindCombox([PhidOcode]);
            BatchBindCombox([user_division]);
            BatchBindCombox([user_product_line]);
            BatchBindCombox([user_regional_center]);
        });
    }

    otype != 'view' && getCntByPt()
    function getCntByPt(){
        if(!PhidTrProj.getValue()) return 
        execServer("GETxs_cnt", { pc: PhidTrProj.getValue() }, function (res) {
            if (res.count == 0) return
            const obj = JSON.parse(res.data)[0].extendObjects;
            console.log(obj, "obj");
            user_xsht_name.setValue(obj.phid);
            user_xsht_no.setValue(obj.bill_no);
            BatchBindCombox([user_xsht_name]);
        });
    }

    PhidTrProj.addListener("helpselected", function () {
        getPtInfo()
        getCntByPt()
    });
    PhidContractno.addListener("change", function () {
        let phid_customer_ent = "";
        console.log(PhidContractno.getValue(), "PhidContractno.getValue()");
        execServer("getCntOrg", { phid: PhidContractno.getValue() }, function (res) {
            if (res.count > 0) {
                try {
                    var obj = JSON.parse(res.data)[0].extendObjects;
                    console.log(obj, "obj");
                    user_sl_organize.setValue(obj.phid_org);
                    PhidDeptno.setValue(obj.user_apply_dept);
                    BatchBindCombox([user_sl_organize]);
                    BatchBindCombox([PhidDeptno]);
                } catch (error) { }
            }
        });
        execServer("searchJfdwByCg", { phid: PhidContractno.getValue() }, (res) => {
            if (res.count == 0 || !res.count) {
                return;
            } else {
                const data = JSON.parse(res.data)[0].extendObjects;
                console.log(data);
                console.log(data.phid_customer_ent);
                mstform.getItem("user_jz_org").setValue(data.fromorg_id);
                BatchBindCombox([mstform.getItem("user_jz_org")]);
                //  execServer("searchJzzz", { phid :`${data.phid_customer_ent}` }, (res) => {
                //     console.log(res);
                //     if (res.count == 0 || !res.count) {
                //       return;
                //     } else {
                //      const data = JSON.parse(res.data)[0].extendObjects
                //      mstform.getItem('user_jz_org').setValue(data.fromorg_id)
                //       BatchBindCombox([mstform.getItem("user_jz_org")]);
                //     }
                //   });
            }
        });

        // 获取合同信息
        getCntMInfoByPhid(mstform.queryById("PhidContractno").getValue());
    });
    if (otype !== "view") {
        dstore.on("datachanged", async function (dstore, eOpts) {
            console.log(`数据发生变化`);
            const records = dstore.getRange();
            for (const record of records) {
                try {
                    console.log(record.data.user_storage_type);

                    if (!record.data.user_storage_type) {
                        //如果为空，则默认为合格默认
                        record.set("user_storage_type", "RKLX0001");
                    }
                    // record.set("user_if_kxys", 0);

                    record.set("user_yfd_rest_qty", record.data.Qty);

                    dgrid.getView().refresh();
                } catch (error) {
                    console.error(error);
                }
            }
        });
    }

    user_bill_type.addListener("change", function () {
        console.log(user_bill_type.getValue(), "PhidContractno.getValue()");
        if (user_bill_type.getValue() == "RKLX0004") {
            mstform.getItem("PhidContractno").userSetMustInput(false);
            mstform.getItem("PhidCompno").userSetMustInput(false);
        } else {
            mstform.getItem("PhidContractno").userSetMustInput(true);
            mstform.getItem("PhidCompno").userSetMustInput(true);
        }
    });
    PhidContractno.on("beforetriggerclick", function (eOp, ignoreBeforeEvent) {
        //选择合同之前先选择项目名称
        if (PhidTrProj.getValue()) {
            // user_yhd_name.setOutFilter({ phid_pc: PhidTrProj.getValue() });
        } else {
            Ext.Msg.alert("提示", "请先选择项目工程");
            mstform.getItem("PhidTrProj").focus();
            return false;
        }
    });
    //要货单根据合同过滤

    user_yhd_name.on("beforetriggerclick", function (eOp, ignoreBeforeEvent) {
        console.log(PhidContractno.getValue());

        if (PhidContractno.getValue()) {
            user_yhd_name.setOutFilter({ phid_contract: PhidContractno.getValue() });
        } else {
            Ext.Msg.alert("提示", "请先选择合同");
            mstform.getItem("PhidContractno").focus();
            return false;
        }
    });
    user_yhd_name.addListener("helpselected", function () {
        execServer("getOrderInfo", { yhd: user_yhd_name.getValue() }, function (res) {
            if (res.count > 0) {
                var obj = JSON.parse(res.data);
                user_yhd_no.setValue(obj[0].extendObjects.no);
                user_yhd_dt.setValue(obj[0].extendObjects.dt);
                user_yhd_amt.setValue(obj[0].extendObjects.yhd_amt);
                PhidContractno.setValue(obj[0].extendObjects.phid);
                ContractName.setValue(obj[0].extendObjects.cnt_name);
                user_cnt_amt.setValue(obj[0].extendObjects.cnt_amt);
                PhidCompno.setValue(obj[0].extendObjects.gys);
                BatchBindCombox([PhidContractno]);
                BatchBindCombox([PhidCompno]);

                console.log(PhidContractno, "PhidContractno");
            }
        });
    });

    /*var user_yhd_name = mstform.getItem('user_yhd_name');
      user_yhd_name.on('beforetriggerclick', function (eOp, ignoreBeforeEvent) {
  user_yhd_name.setOutFilter({ phid_pc: PhidTrProj.getValue() })
  });*/

    PhidWarehouse.addListener("helpselected", function () {
        execServer("getStoreInfo", { store: PhidWarehouse.getValue() }, function (res) {
            if (res.count > 0) {
                var obj = JSON.parse(res.data);
                user_ck_no.setValue(obj[0].extendObjects.no);
                PhidChkpsn.setValue(obj[0].extendObjects.manapsn);
                BatchBindCombox([PhidChkpsn]);
            }
        });
    });

    if (otype == "add") {
        dstore.on("datachanged", function (store, records, index, eOpts) {
            SetTotal();
        });
        dstore.on("add", function (store, records, index, eOpts) {
            SetTotal();
        });
        dstore.on("remove", function (store, records, index, eOpts) {
            SetTotal();
        });
    }
    dgrid.addListener("edit", function (editor, e) {
        var record = e.record;
        if (e.originalValue == e.value) {
            return;
        }
        record.set("user_fpsyje", record.data.TaxMony);
        // record.set("user_yfd_rest_qty", record.data.Qty);
        record.set("user_thsyl", record.data.Qty);

        // record.set("u_fpje");
        SetTotal();
    });

    function SetTotal() {
        let sum_amt = 0;
        dstore.each(function (item) {
            console.log("shcuhujine" + item.get("TaxMony"));
            sum_amt += item.get("TaxMony");
        });
        mstform.getItem("user_rk_amt").setValue(sum_amt);
        dgrid.getView().refresh();
    }

    function getCntMInfoByPhid(phid) {
        if (!phid) return;
        const user_sup_con_person = mstform.queryById("user_sup_con_person");
        execServer("getCntMInfoByPhid", { phid }, (res) => {
            if (res.count == 0) return;
            const _data = JSON.parse(res.data)[0].extendObjects;
            console.log(`合同信息`, _data);
            user_sup_con_person.setValue(_data.gflxr_name);
        });
    }
}
